

package tnviet.proj.jna.stat;

import java.util.Date;
import java.util.Locale;
import java.util.Vector;
import jpcap.packet.Packet;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class PacketStat  extends JNStatisticsController{
    ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);
    long numOfPackets, sizeOfPackets;
    Date first, last;

    final String[] types = {
        resourceManager.getString("totalPackets"),
        resourceManager.getString("totalPacketSize"),
        resourceManager.getString("averagePacketSize"),
        resourceManager.getString("bps"),
        resourceManager.getString("pkts")
    };
    final String[] label = {resourceManager.getString("value")};

    @Override
    public String getName() {
        return resourceManager.getString("overallInfo");
    }

    @Override
    public void analyze(Vector packets) {
        if(packets.size()>0){
            Packet firstP = (Packet)packets.firstElement();
            Packet lastP = (Packet)packets.lastElement();
            first = new Date(firstP.sec*1000 + firstP.usec/1000);
            last = new Date(lastP.sec*1000 + lastP.usec/1000);
        }
        for(int i = 0; i < packets.size(); i++){
            numOfPackets ++;
            sizeOfPackets += ((Packet)packets.elementAt(i)).len;
        }
    }

    @Override
    public void addPacket(Packet p) {
        if(first == null){
            first = new Date(p.sec*1000 + p.usec/1000);
        }
        last=new Date(p.sec*1000+p.usec/1000);
        numOfPackets ++;
        sizeOfPackets += p.len;
    }

    @Override
    public String[] getLabels() {
        return label;
    }

    @Override
    public String[] getStatTypes() {
        return types;
    }

    @Override
    public long[] getValues(int index) {
        long[] ret = new long[1];
        switch(index){
            case 0:
                ret[0] = numOfPackets;
                return ret;
            case 1:
                ret[0] = sizeOfPackets;
                return ret;
            case 2:
                if(numOfPackets == 0) {
                    ret[0] = 0;
                } else {
                    ret[0] = sizeOfPackets/numOfPackets;
                }
                return ret;
            case 3:
                if(first == null){
                    ret[0] = 0;
                } else {
                    long sec = (last.getTime() - first.getTime())/1000;
                    if(sec == 0) ret[0] = 0;
                    else {
                        ret[0] = sizeOfPackets*8/sec;
                    }
                }
                return ret;
            case 4:
                if(first == null){
                    ret[0] = 0;
                } else {
                    long sec = (last.getTime() - first.getTime())/1000;
                    if(sec == 0) ret[0] = 0;
                    else {
                        ret[0] = numOfPackets/sec;
                    }
                }
                return ret;
            default:
                return null;
        }
    }

    @Override
    public void clear() {
        numOfPackets = 0;
        sizeOfPackets = 0;
        first = null;
        last = null;
    }

}
